/*****************************提交*********************/
    var baseId=GetQueryString('baseid')
    var applyType=GetQueryString('atype')
    var caseStatus=GetQueryString('cstatus')
    var tempType
    switch (applyType){
    	case 'law':
    	   $('h1.mui-title').html('申请法律援助')
    	   tempType='entrust_law'
    		break;
        case 'adult':
            $('h1.mui-title').html('申请合适成年人')
            tempType='entrust_adult'
            break;
        case 'survey':
           $('h1.mui-title').html('申请社会调查')
           tempType='entrust_survey'
            break;
    	default:
    		break;
    }
    //获取对应模版
    var getOneTempData={
        url:'schedule_manage/sendLetter',
        fromwho:'ld',
        sendData:{
            type:tempType,
            base_msg_id:baseId
//          type:'entrust_survey'
        }
    }
    var letter_id,letter
    getAjax(getOneTempData,
        function(data){
            if(data.result==1){
                var tempData=decodeHtml(data.data.letter_file)
                letter_id=data.data.letter_id
                letter=data.data.letter
                var reg=/\{\{(.+?)\}\}/g
                var resultArr=[];
                while(result=reg.exec(tempData)){
                    if($.inArray(result[1],resultArr)==-1){
                        resultArr.push(result[1]);
                    }
                }
                var html='';
                if(resultArr.length>0){
                    var newArr=changetempname(resultArr)
                    console.log(newArr)
                    html+=`<form class="mui-input-group" id="sendFormTemp">`
                    $.each(newArr,function(k,v){
                        html+=`
                            <div class="mui-input-row">
                                <label>${v}</label>
                            <input type="text" name="${k}" class="mui-input-clear" >
                            </div>
                        `
                    })
                    html+=`</form>`
                }else{
                    html+=`<p style="margin:20px auto;text-align:center">委托函无需输入内容,可直接提交<p>`
                }
                html+=`<button type="button" class="mui-btn mui-btn-primary" id="showtemp" style="margin: 20px auto;display: block;">预览函件</button>`
                $('.mui-content').html(html)
                $(document).on('click','#showtemp',function(){
                    var showjson={}
                    $('#sendFormTemp').find('input').each(function(k,v){
                        var key=$(v).attr('name')
                        var val=$(v).val()
                        showjson[key]=val
                    })
                    var showhtml = template.render(tempData, showjson);
                    mui.alert(showhtml,'预览模版','','','div')
                })
            }else{
                 var html=`<p style="margin:20px auto;text-align:center">暂无委托函,可直接提交<p>`
                 $('.mui-content').html(html)
            }
        },
        function(data){
            console.log(data)
        }
    )
    $('#surepsyer').on('click',function(){
        $('#sendFormTemp').find('input').each(function(k,v){
            var key=$(v).attr('name')
            var val=$(v).val()
            letter[key]=val
        })
        
        var upApplyData={
            url:'schedule_manage/addScheduleAPi',
            fromwho:'ld',
            sendData:{
                base_msg_id:baseId,
                apply_type:applyType,
                case_status:caseStatus,
                last_level:0,
                letter:letter,
                letter_id:letter_id
            }
        }
        console.log(upApplyData)
        var waitingDialog
        mui.plusReady(function(){
            waitingDialog = plus.nativeUI.showWaiting("提交中");
        })
        upAjax(upApplyData,
            function(data){
                mui.plusReady(function(){
                    waitingDialog.close();
                })
                if(data.result==1){
                    mui.toast('委托成功')
//                  console.log(document.referrer)
                    setTimeout(function(){
                        document.location.href = localStorage.getItem('casebackurl')
                    },500)
                }else{
                    mui.toast(data.msg)
                }
            },
            function(data){
                console.log(data)
            }
        )
    })
/*****************************提交end*********************/